﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainDriver.DomainModeling.DataProviders;
using DomainDriver.DomainModeling.DomainModels;
using DomainDriver.DomainModeling.DomainModels.ConstructionResources;
using DomainDriver.DomainModeling.Repositories;
using AutomotivePricingModel.ValueObjects.YearlyFinancialAssessments;

namespace AutomotivePricingModel.ValueObjects.UnitTests.YearlyFinancialAssessments
{
    public class TestDomainModel : DomainDriver.DomainModeling.DomainModels.DomainModel, IBatchPersistableDomainModel<TestDataSet>
    {
        #region Construction Code

        public TestDomainModel(DataSourcePersistenceType persistenceType, bool buildModelWithConstraints)
            : this(persistenceType, InvalidCallAction.Ignore, buildModelWithConstraints)
        { }

        public TestDomainModel(DataSourcePersistenceType persistenceType, InvalidCallAction invalidCallAction, bool buildModelWithConstraints)
            : base(persistenceType, invalidCallAction, buildModelWithConstraints)
        {
            RegisterDataSource<TestDataSet>(DataSourcePersistenceType.File, RepositoryConstructionMethods.CreateDataSetBackedRepository);
            RegisterDataSource<TestDatabaseContext>(DataSourcePersistenceType.Database, RepositoryConstructionMethods.CreateEntityFrameworkBackedRepository);

            RegisterDomainObjectRepository<Guid, YearlyFinancialAssessment>();

            YearlyFinancialAssessment.IsAggregateRoot = true;

            CreateConstraints += ConstraintCreationHandler;

            Initialize();
        }

        private void ConstraintCreationHandler(object sender, EventArgs args)
        { }

        #endregion

        public void LoadFromDataSource(TestDataSet dataSource)
        {
            object dataSourceAsObject = dataSource;
            LoadFromDataSource(dataSourceAsObject);
        }

        public void PersistToDataSource(TestDataSet dataSource)
        {
            object dataSourceAsObject = dataSource;
            PersistToDataSource(dataSourceAsObject);
        }
    }

    internal static class ExtensionMethods
    {
        internal static IRepository<Guid, YearlyFinancialAssessment> YearlyFinancialAssessments(this IDomainModel domainModel)
        {
            return domainModel.GetRepository<Guid, YearlyFinancialAssessment>();
        }
    }
}